class CatalogController {

  def index = { redirect(action: list, params: params) }

  // the delete, save and update actions only accept POST requests
  static allowedMethods = [delete: 'POST', save: 'POST', update: 'POST']

  def list = {
    params.max = Math.min(params.max ? params.max.toInteger() : 20, 100)
    if(params.id){
      def catalog = Catalog.get(params.id)
      def catalogList = Catalog.findAllByParent(catalog, [max:100])
      params.order = 'asc'
      params.sort = 'id'
      def entryInstanceList = Entry.findAllByCatalog(catalog, params)
      def entryInstanceTotal = Entry.countByCatalog(catalog)
      return [catalogInstance:catalog,catalogInstanceList: catalogList, entryInstanceList:entryInstanceList, entryInstanceTotal:entryInstanceTotal]
    } else {
      def  catalogInstanceList = Catalog.findAllByParentIsNull()
      return [catalogInstanceList: catalogInstanceList, catalogInstanceTotal: Catalog.count()]
    }
  }

  def show = {
    def catalogInstance = Catalog.get(params.id)

    if (!catalogInstance) {
      flash.message = "Catalog not found with id ${params.id}"
      redirect(action: list)
    }
    else { return [catalogInstance: catalogInstance] }
  }

  def delete = {
    def catalogInstance = Catalog.get(params.id)
    if (catalogInstance) {
      try {
        catalogInstance.delete()
        flash.message = "Catalog ${params.id} deleted"
        redirect(action: list)
      }
      catch (org.springframework.dao.DataIntegrityViolationException e) {
        flash.message = "Catalog ${params.id} could not be deleted"
        redirect(action: show, id: params.id)
      }
    }
    else {
      flash.message = "Catalog not found with id ${params.id}"
      redirect(action: list)
    }
  }

  def edit = {
    def catalogInstance = Catalog.get(params.id)

    if (!catalogInstance) {
      flash.message = "Catalog not found with id ${params.id}"
      redirect(action: list)
    }
    else {
      return [catalogInstance: catalogInstance]
    }
  }

  def update = {
    def catalogInstance = Catalog.get(params.id)
    if (catalogInstance) {
      if (params.version) {
        def version = params.version.toLong()
        if (catalogInstance.version > version) {

          catalogInstance.errors.rejectValue("version", "catalog.optimistic.locking.failure", "Another user has updated this Catalog while you were editing.")
          render(view: 'edit', model: [catalogInstance: catalogInstance])
          return
        }
      }
      catalogInstance.properties = params
      if (!catalogInstance.hasErrors() && catalogInstance.save()) {
        flash.message = "Catalog ${params.id} updated"
        redirect(action: show, id: catalogInstance.id)
      }
      else {
        render(view: 'edit', model: [catalogInstance: catalogInstance])
      }
    }
    else {
      flash.message = "Catalog not found with id ${params.id}"
      redirect(action: edit, id: params.id)
    }
  }

  def create = {
    def catalogInstance = new Catalog()
    catalogInstance.properties = params
    return ['catalogInstance': catalogInstance]
  }

  def save = {
    def catalogInstance = new Catalog(params)
    if (!catalogInstance.hasErrors() && catalogInstance.save()) {
      flash.message = "Catalog ${catalogInstance.id} created"
      redirect(action: show, id: catalogInstance.id)
    }
    else {
      render(view: 'create', model: [catalogInstance: catalogInstance])
    }
  }
}
